package com.cwj.mansysback.servlet.impl;

import com.cwj.mansysback.entity.Course;
import com.cwj.mansysback.entity.LocalAccount;
import com.cwj.mansysback.service.api.CourseService;
import com.cwj.mansysback.service.impl.CourseServiceImpl;
import com.cwj.mansysback.servlet.util.MyUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class GetCourseServlet extends HttpServlet {

    private  static final CourseService courseSer = new CourseServiceImpl();

    private static final String keys[]={"cid","tid"};

   private static class CourseStatement extends  Statement{
        Course course =null;
        List<Course> courseList=null;

        Long count;

       public Long getCount() {
           return count;
       }

       public void setCount(Long count) {
           this.count = count;
       }

       public CourseStatement(Integer statue) {
           super(statue);
       }

       public Course getCourse() {
           return course;
       }

       public void setCourse(Course course) {
           this.course = course;
       }

       public List<Course> getCourseList() {
           return courseList;
       }

       public void setCourseList(List<Course> courseList) {
           this.courseList = courseList;
       }
   }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        LocalAccount loAcc = new LocalAccount();

        final String aid = loAcc.getAid();
        final Integer accrank = loAcc.getAccrank();

        CourseStatement courseStatement =new CourseStatement(0);

        String id =null;

        if(req.getRequestURI().contains("/Lock/GetCourse") && (id = req.getParameter(GetCourseServlet.keys[0])) != null){


            courseStatement.setCourse(GetCourseServlet.courseSer.selectCourse(id));

        }else if(req.getRequestURI().contains("/Lock/GetTeaCourse") && (id = req.getParameter(GetCourseServlet.keys[1])) != null){


            courseStatement.setCourseList(GetCourseServlet.courseSer.selectCourseTea(id));

        }else if(req.getRequestURI().contains("/Lock/GetSomeCourses") && req.getParameter("pageInd") != null && req.getParameter("rowCount") != null){

            Integer pageInd = Integer.valueOf(req.getParameter("pageInd"));
            Integer rowCount = Integer.valueOf(req.getParameter("rowCount"));
            String orderKey = req.getParameter("orderKey");
            String isAsc = req.getParameter("isAsc");

            courseStatement.setCourseList(GetCourseServlet.courseSer.selectSomeCourses(pageInd,rowCount,orderKey,(isAsc == null || Boolean.parseBoolean(isAsc))));

            courseStatement.setCount((long) GetCourseServlet.courseSer.selectSomeCourses(1,Integer.MAX_VALUE,orderKey,(isAsc == null || Boolean.parseBoolean(isAsc))).size());
        }else{

            MyUtils.writeResponse(resp,new Statement(7));

            return ;
        }

        MyUtils.writeResponse(resp,courseStatement);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
}
